import request from '@/utils/request'
import _ from "lodash";
const path = require('path')
const files = require.context('.', true, /\.js/)
const modules = {}
files.keys().forEach(key => {
    const name = path.basename(key, '.js')
    if(name === 'index'){ return; }
    modules[name] = files(key).default || files(key)
})

//
_.api = function(name,options = {}){
    let fun = _.get(modules,name);
    if(options.params){
        options.params.jpressAppId = "app_test";
        options.params.ct = new Date().getTime();
        options.params.sign = _.sign(options.params);
    }
    //console.log(options);
    //
    if(typeof fun == "string"){
        options.url = fun;
    }else{
        options = Object.assign({
            headers: {
                'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
            },
            responseType:'json',
            method: 'get',
            url: '',
            params:options.params
        },fun(options));
    }
    //
    return request(options)
}
